﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using CsharpTrainer.NHibernate3;
using CsharpTrainer.NHibernate3.Mapping;
using CsharpTrainer.NHibernate3.Entities;
using Zeus.Thunder.Core;
using NHibernate;

namespace SharpTrainer.NHibernate
{
    class AddCategoryTest : ITestCase
    {
        public void Run()
        {
            string connStr = ConfigurationManager.ConnectionStrings["NH3TrainConnStr"].ConnectionString;

            try
            {
                NHibernateDb<ArticleMap> db = new NHibernateDb<ArticleMap>(connStr);
                
                Console.Write("Please Input Category Name: ");
                string name = Console.ReadLine();

                ISessionFactory factory = db.SessionFactory;
                using (var session = factory.OpenSession())
                {
                    var category = new Category
                                       {
                                           Name = name
                                       };
                    session.Save(category);

                    Console.WriteLine("Add Category Successfully!");

                    Console.Write("Please Input Article Name: ");
                    string name2 = Console.ReadLine();

                    var article = new Article
                                      {
                                          Name = name2,
                                          Category = category
                                      };
                    session.Save(article);

                    Console.WriteLine("Add Article Successfully!");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.ToString());
            }
        }
    }
}
